package com.dayuanit.dy15.spider.tree;

public class TestTree {

    static class Node {
        //节点保存的数据
        public Object data;
        public Node left;
        public Node right;

        public Node(Object data, Node left, Node right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }

    //前序遍历
    static void preLoop(Node root) {
        if (root == null) {
            return;
        }

        System.out.println(root.data);//根节点的值
        preLoop(root.left);
        preLoop(root.right);
    }

    //中序遍历
    static void middleLoop(Node root) {
        if (root == null) {
            return;
        }

        middleLoop(root.left);
        System.out.println(root.data);//根节点的值
        middleLoop(root.right);
    }

    public static void main(String[] args) {
        //构建一颗二查查找树
        Node f = new Node("f", null, null);
        Node c = new Node("c", null, f);
        Node a = new Node("a", null, c);
        Node k = new Node("k", null, null);
        Node kk = new Node("k", a, k);
        Node t = new Node("t", null, null);
        Node w = new Node("w", t, null);
        Node root = new Node("m", kk, w);


        //前序遍历
//        preLoop(root);

        //中序
        middleLoop(root);
    }
}
